Giter Club home page Giter Club logo

khome's Introduction

GitHub Actions status LINE

Khome

Khome is a smart home automation library for Home Assistant, written in Kotlin. It enables you to write your own Home Assistant automation applications, that can observe state changes, listen to events, and much more. Khome was written with safeness in mind. That means we wrote Khome with a fail first approach. See more about this in the "Safety's first Section".

Simple Example:

val KHOME = khomeApplication()

val LivingRoomMotion = KHOME.LuminanceSensor("livingRoom_motion".objectId)
val LivingRoomMainLight = KHOME.RGBWLight("livingRoom_main_light".objectId)

fun main() {
    LivingRoomMotion.attachObserver { //this: Sensor<SwitchableState,MotionSensorAttributes>
        if (measurement.value == SwitchableValue.ON) {
            LivingRoomMainLight.desiredState = RGBWLightState(ON, colorTemp = 5000.kelvin)
        }
    }

    KHOME.runBlocking()
}

In this little example, we observed the motion sensor in the living room and when the sensors motion alarm turns to on, we change the state of the main light in the living room to ON. As you can see here, Khome encourages you to think in states rather than services you have to call. This is less error-prone and helps the developer to stay in the mindset of states. This distinguishes Khome from most other automation libraries.

Khome comes with a lot of predefined factory functions, data classes, observers and more for generic entity types. To achieve the same result than shown above, we can also use Khomes higher-level API which lets you write concise code with ease.

val KHOME = khomeApplication()

val LivingRoomMotion = KHOME.LuminanceSensor("livingRoom_motion".objectId)
val LivingRoomMainLight = KHOME.SwitchableLight("livingRoom_main_light".objectId)

fun main() {
    LivingRoomMotion.onMotionAlarm { //this: MotionSensor
        LivingRoomMainLight.setColorTemperature(5000.kelvin)
    }

    KHOME.runBlocking()
}

Home Assistant

HA is an open-source home-automation platform written in Python 3 that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server.

If you're not already familiar with Home Assistant, you find all you need on the Getting Started page.

If you are from...

... the Kotlin World:

Since Home Assistant is written in Python 3, you may ask yourself if you need to write Python code on the Home Assistant side. But you don't have to. All you need to do is configuring it via .yaml files and/or the user interface. But you need to install and run it on your own server. There is plenty of information and tutorials on the web to support you with that. Google will help you. Also, there is a Discord channel to get in touch easily with the community.

... the Python World:

Yes, you need to learn Kotlin. It is definitely worth a try. In my opinion, it is worth even more. But that's a different story. Probably the fastest way for you to get into Kotlin is the Kotlin for Python Introduction from the official Kotlin documentation. Here is a list of the most important Kotlin online resources.

Installation

Home Assistant

Further information on this topic is available on the official Home Assistant Documentation page.

Khome

For now, you can use Jitpack to install Khome locally. Just add the following lines to your build.gradle or maven file.

Gradle

repositories {
    // ...
    maven { url "https://jitpack.io" }
}
dependencies {
    // ...
    implementation 'com.github.dennisschroeder:khome:${replace with a version}'
}

Maven

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
        <groupId>com.github.dennisschroeder</groupId>
        <artifactId>khome</artifactId>
        <version>${replace with a version}</version>
</dependency>

Documentation

Khome has no opinion on how you want to run your application, what other libraries or pattern you choose, or what else is best for what you like to build. All Khome needs is a Kotlin environment to run properly.

Again, if you are new to Kotlin, you might check out Getting Started with IntelliJ IDEA or Working with the Command Line Compiler. I recommend using Kotlin with Intellij IDEA to get started. It's the best way to get into it. You can download the free Community Edition from JetBrains.

Working with Khome

Credits

Even though the idea to build Khome and the implementation was made by me, you'll find the word "we" quite often in the documentation. That is because I had the pleasure to work with Tobias Hermann on this project who served me as a sparring partner by challenging my ideas and design decisions, introducing new ideas and designs, reviewing some parts of the code, and an overall consultant in the field of software engineering. So using I in the documentation would just be incorrect.

Therefore, a special thank goes out to Tobias.

Check out his awesome work, especially frugally-deep, functionalPlus, and his thoughts on programming. Oh and leave a star or two ;-).

khome's People

Contributors

dennisschroeder avatar dobiasd avatar hendrikstill avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

khome's Issues

Koin cant find HassApiClient

Exception in thread "main" org.koin.core.error.NoBeanDefFoundException: No definition found for 'khome.communicating.HassApiClient' has been found. Check your module definitions.
	at org.koin.core.scope.Scope.findDefinition(Scope.kt:170)
	at org.koin.core.scope.Scope.resolveInstance(Scope.kt:164)
	at org.koin.core.scope.Scope.get(Scope.kt:128)
	at khome.KhomeApplicationImpl$special$$inlined$inject$default$2.invoke(Scope.kt:327)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at khome.KhomeApplicationImpl.getHassApi(KhomeApplicationImpl.kt:62)
	at khome.KhomeApplicationImpl.callService(KhomeApplicationImpl.kt:139)
	at khome.KHmoeMainKt.main(KHmoeMain.kt:25)
	at khome.KHmoeMainKt.main(KHmoeMain.kt)
fun main() {
    KHOME.notifyMobileApp(Device.from("mmy_phone")) {
        title = "INTRUDER ALARM"
        message = "Garden shed door opened"
    }
    KHOME.runBlocking()
}

Feature Discussion: Kotlin Multiplatform

I'm interested in using Khome in a mobile app that runs on both iOS and Android using Kotlin Multiplatform. Would the collaborators (y'all) be interested in making this project multiplatform compatible? Notably this would mean swapping any incompatible dependencies with compatible versions, like KotlinLogging -> Kermit and Gson -> kotlinx.serialization. I'm happy to help with the work, but can also easily maintain a fork if this isn't something wanted in the main repo.

Additionally, if there are any changes to make Khome usable as a library in a mobile app then I'd want to make those changes as well (in case that affects the fork vs. no-fork decision).

Very cool project, and thanks!

[Feature Request] Hass.io (Operating System) Add-on + Usability concerns

First of all, I'd like to say thank you and well done!
I'm a great Kotlin fan myself and while I was using AppDaemon for my automations, all this loosely typed nonsense is driving me nuts. I'm very interested to be able to automate parts of my home using a proper programming language like Kotlin.
I am wondering however how you're currently running khome to test it. I understand it can be run from any device that can run Java, so running it from hass.io shouldn't be a problem. However, hass.io does however require an add-on (because of docker and such) to be able to run it, turn it on/off and update it eventually (like AppDaemon).
So that's my humble request for now, other than that, keep up the terrific work :D

Edit: I am a bit curious why it is necessary to create all these classes for simple actions like turning on a certain light. I mean, shouldn't

callService(TurnOn(Light("wall_lamp")))
// Or even
Light("wall_lamp").turnOn()

be a lot easier and lower the learning curve from the python people we're trying to convince to switch?
I assume it's necessary for that Koin stuff, but it does increase the difficulty of using it a lot I think.
Anyways I'll have to play with it to be sure of course.

Edit2: After playing around a bit, I do think that using Koin to make the code more DSL like is hurting the actual usability of the library. To turn off, say 10 light, instead of just creating Light objects from a list of entities and turning those off with a single forloop, now I'd have to create a class for any light in the house, plus for each light a TurnOffLight and TurnOnLight class, I'd have to register them all as beans and then I'd have to callService<>() manually for each light!
Plus, when I add a new light, I'd have to do it all again!
I think some things of your library are great and the amount of work you've put into it is clear, but I think it wouldn't hurt to pursue a simpler approach, like AppDaemon. Abstract away from the service calls and make entities instances. At least that's how I would do it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.