Skip to content

Commit 38ddaad

Browse files
committed
Added power usage entities, added dynamic data updating, fixed bugs.
1 parent 1076ef6 commit 38ddaad

17 files changed

Lines changed: 977 additions & 268 deletions

README.md

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,59 @@
11
[![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs)
22
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/crownstone/crownstone-home-assistant?label=Latest%20release&style=for-the-badge)](https://github.com/crownstone/crownstone-home-assistant/releases)
33
[![Maintained](https://img.shields.io/maintenance/yes/2020?style=for-the-badge)](https://github.com/RicArch97)
4-
![Github All Releases](https://img.shields.io/github/downloads/crownstone/crownstone-home-assistant/total.svg?label=Total%20downloads&style=for-the-badge&color=yellow)
54

65
# Crownstone Home Assistant Integration
76

8-
This is the HACS repository for the Crownstone Home Assistant integration!
7+
<img src="images/crownstone_home_assistant.png" width="1628" height="763.5" />
98

10-
This repo contains files that are not available yet in Home Assistant Core. The main purpose is to make the integration available for testing. If you're using this integration and encounted a bug, please post an issue with the problem so we can resolve it.
9+
# Features
10+
11+
Welcome to the Crownstone integration, a very advanced integration for Home Assistant!
12+
13+
The supreme versatility and user friendliness of the Crownstones is reflected into this integration by the following features:
1114

12-
Also, if you have any great ideas to make the integration even better, let us know!
15+
* Extremely easy to set up
16+
* Instant updates for switch states and data
17+
* Dynamically adding and removing devices/entities
18+
* Real-time power usage measurements using the Crownstone USB
19+
* Keeps track of **who** is in which room!
20+
* Custom advanced presence triggers to make your whole home react to your presence!
1321

14-
<img src="images/dashboard.jpg" width="216" height="468" /> <img src="images/device_triggers.jpg" width="216" height="468" /> <img src="images/trigger_config.jpg" width="216" height="468" />
22+
This integration stays in one line with the Crownstone app. When data is updated, Crownstones added/deleted or Locations added/deleted, the changes are **instantly** made in Home Assistant as well, thanks to our amazing Server Send Event service!
1523

1624
# Installation
1725

1826
## HACS
1927

2028
Visit the [HACS installation page](https://hacs.xyz/docs/installation/manual) to set up HACS in your Home Assistant.
2129

22-
Click the 3 dots button in the top right corner, and click custom respositories. Select category integration, and add the link of this repository. In the HACS store, click the "+" button in the bottom right corner, and search for Crownstone.
30+
Click the 3 dots button in the top right corner, and click custom repositories. Select category integration, and add the link of this repository. In the HACS store, click the "+" button in the bottom right corner, and search for Crownstone.
2331

2432
## Manually
2533

2634
Copy all files from custom_components/crownstone/ to custom_components/crownstone inside your HA config folder.
2735

28-
# Features
29-
30-
The Crownstone integration currently supports the following features:
31-
32-
## Switching and dimming Crownstones
36+
# Crownstones
3337

34-
Crownstone are respresented in the light platform. You can create a card in the overview and add your Crownstone entities to have a nice overview of your Crownstones! If a Crownstone supports dimming, there will be a brightness slider to dim your Crownstone.
38+
Crownstone are represented in the light platform, and can switch or dim. You can create a card in the overview and add your Crownstone entities to have a nice overview of your Crownstones! If a Crownstone supports dimming, there will be a brightness slider to dim your Crownstone.
3539

36-
The default communication method to switch and dim Crownstones, is the cloud. Using a cloud, it can take 1-3 seconds before a crownstone switches. However, we added support for the Crownstone USB in Home Assistant. Plug the USB in and start Home Assistant, it should automatically start switching using the USB. Since the latencies are so low using the USB, switching and dimming will be instant!
40+
When the ability state of **dimming** is changed through the Crownstone app, your config entry will reload to process the change in supported features. This means that some states may be different, and the power usage is likely back to 0W.
3741

38-
### Crownstone ability updates
42+
Crownstone entities have some state attributes to provide more information about the Crownstone. To see this information, simply click on the entity. This information is:
43+
* Switch method (Crownstone Cloud or Crownstone USB Dongle)
44+
* Dimming (Enabled or Disabled)
45+
* Tap To Toggle (Enabled or Disabled)
46+
* Switchcraft (Enabled or Disabled)
3947

40-
Currently, the Crownstone integration supports updating a Crownstone's ability during runtime. Once changed in the app, Home Assistant will reload your config entry, and re-initialize all your Crownstone entities.
48+
<img src="images/crownstone_entity.png" width="390" height="385" />
4149

42-
## Presence sensor
50+
# Presence
4351

4452
The unique selling point of Crownstone, the presence on room level, is also available in Home Assistant!
4553

46-
The state sensor is a string of the first names of the people who are in the room. It is possbile for multiple people to be in the same room, the names of the users is separated by a comma.
54+
The state sensor is a string of the first names of the people who are in the room. It is possible for multiple people to be in the same room, the names of the users is separated by a comma.
4755

48-
Apart from the room presence there is also sphere presence. this shows who is currently in the sphere (house, apartement). If a user is at home (in the sphere), the user's name will be shown in sphere presence and one of the room presence entities.
56+
Apart from the room presence there is also sphere presence. this shows who is currently in the sphere (house, apartment). If a user is at home (in the sphere), the user's name will be shown in sphere presence and one of the room presence entities.
4957

5058
The Crownstone app is leading the presence functionality, for any issues with your presence detection make sure to go to your Crownstone app and retrain your rooms. 4 Crownstones are required for the localisation on room level. If you don't have 4 Crownstones, it will only show your presence in the sphere (house).
5159

@@ -74,16 +82,74 @@ Note that these triggers are `event based`. Multiple users and all users trigger
7482

7583
The events are registered, which means that if a user enters a room, but leaves shortly after, the event for entered stays registered. If the other 2 users in the trigger then enter the room, the action will STILL execute. Make sure you select the correct trigger for the occasion. Using a single user trigger to execute an action will always be the safest option.
7684

77-
# Troubleshooting
85+
<img src="images/device_triggers.jpg" width="216" height="468" /> <img src="images/trigger_config.jpg" width="216" height="468" />
86+
87+
# Power usage
88+
89+
Crownstone's live power usage streaming is also available in Home Assistant. Because of the constant updates, this functionality is only available when using the [Crownstone USB dongle](#crownstone-usb-dongle).
90+
91+
The power usage for each Crownstone updates every minute, or instantly for a particular Crownstone when switching it. It can take some time before the correct power usage is displayed.
92+
93+
## Power usage device triggers
94+
95+
Power usage entities use the default device triggers from sensor for power usage sensors. The following triggers are available:
96+
- Crownstone Power usage energy changes
97+
98+
For `Crownstone Power usage changes` there are 3 options:
99+
- Above a certain value
100+
- Below a certain value
101+
- Duration of the change in hh/mm/ss
102+
103+
You can have other devices react to peaks in power usage, send an event or notification, whatever you like!
104+
105+
<img src="images/power_usage.png" width="356" height="479" />
106+
107+
# Comparison
108+
109+
The integration works with the Crownstone Cloud and the Crownstone USB dongle. The differences between the two are only relevant for the Crownstones. The integration uses the Crownstone Cloud by default, to use the Crownstone USB you'll have to purchase one from the Crownstone store.
110+
111+
Presence updates and data updates are always done using the Crownstone Cloud.
112+
113+
## Crownstone Cloud
114+
115+
- [x] Switching Crownstones
116+
- [x] Dimming Crownstones
117+
- [x] State updates in Home Assistant when switching from Crownstone app
118+
- [x] Can switch multiple Crownstones at once
119+
- [ ] No delay when switching Crownstones
120+
- [ ] State updates in Home Assistant when using lightswitch with Switchcraft
121+
- [ ] Can switch Crownstones independently (no smartphone in proximity required)
122+
- [ ] Can use power usage entities
123+
124+
## Crownstone USB Dongle
125+
126+
- [x] Switching Crownstones
127+
- [x] Dimming Crownstones
128+
- [x] State updates in Home Assistant when switching from Crownstone app
129+
- [x] Can switch multiple Crownstones at once
130+
- [x] No delay when switching Crownstones
131+
- [x] State updates in Home Assistant when using lightswitch with Switchcraft
132+
- [x] Can switch Crownstones independently (no smartphone in proximity required)
133+
- [x] Can use power usage entities
134+
135+
Get your Crownstone USB dongle [here](https://shop.crownstone.rocks/products/crownstone-usb-dongle) and enhance your Home Assistant experience!
78136

79-
1. When I switch my Crownstone using the app, the change isn't visible in Home Assistant, why?
137+
# Testing
80138

81-
We chose not to update the state in Home Assistant externally because the state isn't always known. When using switchcraft for example, the state of the Crownstone is not updated in the cloud. The safest option is to only let Home Assistant change the state of it's entities. The Crownstone entities use assumed state which means you will ALWAYS be able to switch your Crownstones using Home Assistant, independed of the state in the app.
139+
To ensure the best possibly experience with the Crownstone Home Assistant integration, thoroughly testing is required. You can find a test rapport [here](test_rapport.md).
82140

83-
2. I updated some data / Crownstone firmware in the Crownstone app, but it is not updated in Home Assistant
141+
This rapport shows exactly what tests have been done, and what the result was.
142+
If you like to contribute test results of tests that have not been done by us yet, you can post an issue with the test & the result. We will adjust the rapport accordingly including credit.
84143

85-
Dynamically updating data is not yet supported for the Crownstone integration. To register the changes in HA, you'll have to restart.
144+
# Roadmap
86145

87-
3. Will there be an option to use power usage as trigger in Home Assistant?
146+
- [ ] Publish initial Crownstone integration to Home Assistant Core (in progress)
147+
- [x] Optimize configuration flow for easier setup
148+
- [x] Create device triggers for Presence devices
149+
- [x] Add power usage entities to Crownstone devices
150+
- [x] Fix state updates coming from the Crownstone app not being done in Home Assistant
151+
- [x] Dynamically update data & add/remove Crownstone and Location devices without restarting or reloading
152+
- [ ] Create device conditions for Presence devices
153+
- [ ] Optimize power usage accuracy by implementing new UART protocol
88154

89-
The power usage feature is on the roadmap for upcoming updates. Likely this will come whenever the Crownstone hub is finished.
155+
Any ideas for future updates? Let us [know](mailto:ask@crownstone.rocks?subject=[GitHub]%20Crownstone%20Home%20Assistant%20Integration)!

custom_components/crownstone/config_flow.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ async def async_step_user(self, user_input=None):
4444
self.cloud = CrownstoneCloud(
4545
email=user_input[CONF_EMAIL],
4646
password=user_input[CONF_PASSWORD],
47-
websession=aiohttp_client.async_get_clientsession(self.hass),
47+
clientsession=aiohttp_client.async_get_clientsession(self.hass),
4848
)
4949

5050
# handle login errors on setup form
5151
try:
52-
await self.cloud.async_login()
52+
await self.cloud.async_initialize()
5353
# save email and password for later use
5454
self.login_info = user_input
5555
# start next flow
@@ -75,19 +75,16 @@ async def async_step_user(self, user_input=None):
7575

7676
async def async_step_sphere(self, user_input=None):
7777
"""Handle the step for selecting a sphere."""
78-
# get the spheres for the user
79-
await self.cloud.spheres.async_update_sphere_data()
80-
8178
# only 1 sphere configured, don't show form and set this as sphere
82-
if len(self.cloud.spheres.spheres) == 1:
79+
if len(self.cloud.cloud_data.spheres) == 1:
8380
user_input = {
84-
CONF_SPHERE: next(iter(self.cloud.spheres.spheres.values())).name
81+
CONF_SPHERE: next(iter(self.cloud.cloud_data.spheres.values())).name
8582
}
8683

8784
# show form with drop down menu
8885
if user_input is None:
8986
# generate sphere list
90-
for sphere in self.cloud.spheres:
87+
for sphere in self.cloud.cloud_data:
9188
self.spheres.append(sphere.name)
9289

9390
return self.async_show_form(
@@ -100,9 +97,6 @@ async def async_step_sphere(self, user_input=None):
10097
# make sure this sphere is only set up once
10198
self._abort_if_unique_id_configured()
10299

103-
# cleanup RequestHandler
104-
self.cloud.reset()
105-
106100
# return data to main
107101
return self.async_create_entry(
108102
title=self.unique_id,

custom_components/crownstone/const.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,44 @@
55
SENSOR_PLATFORM = "sensor"
66
LIGHT_PLATFORM = "light"
77

8-
# Signals
9-
SIG_STATE_UPDATE = "crownstone.state_update"
8+
# Signals (within integration)
9+
SIG_PRESENCE_STATE_UPDATE = "crownstone.presence_state_update"
10+
SIG_PRESENCE_UPDATE = "crownstone.presence_update"
11+
SIG_CROWNSTONE_STATE_UPDATE = "crownstone.crownstone_state_update"
12+
SIG_CROWNSTONE_UPDATE = "crownstone.crownstone_update"
13+
SIG_POWER_STATE_UPDATE = "crownstone.power_state_update"
14+
SIG_POWER_UPDATE = "crownstone.power_update"
1015
SIG_TRIGGER_EVENT = "crownstone.trigger_event"
16+
SIG_ADD_CROWNSTONE_DEVICES = "crownstone.add_crownstone_device"
17+
SIG_ADD_PRESENCE_DEVICES = "crownstone.add_presence_device"
18+
SIG_UART_READY = "crownstone.uart_ready"
19+
20+
# Added/deleted device or entities
21+
ADDED_ITEMS = "added_items"
22+
REMOVED_ITEMS = "removed_items"
23+
ABILITY = {"enabled": False, "properties": {}}
24+
25+
# Abilities state
26+
ABILITY_STATE = {True: "Enabled", False: "Disabled"}
1127

1228
# Config flow
1329
CONF_SPHERE = "sphere"
1430

1531
# Crownstone entity
1632
CROWNSTONE_TYPES = {
17-
"PLUG": "Crownstone plug",
18-
"CROWNSTONE_USB": "Crownstone USB",
19-
"BUILTIN": "Crownstone built-in",
20-
"BUILTIN_ONE": "Crownstone built-in one",
21-
"GUIDESTONE": "Crownstone guidestone",
33+
"PLUG": "Plug",
34+
"CROWNSTONE_USB": "USB Dongle",
35+
"BUILTIN": "Built-in",
36+
"BUILTIN_ONE": "Built-in One",
37+
"GUIDESTONE": "Guidestone",
2238
}
2339
CROWNSTONE_EXCLUDE = ["CROWNSTONE_USB", "GUIDESTONE"]
2440

2541
# Presence entity
26-
PRESENCE_SPHERE = {"icon": "mdi:earth", "description": "Sphere presence"}
42+
PRESENCE_SPHERE = {"icon": "mdi:earth", "description": "Sphere Presence"}
2743
PRESENCE_LOCATION = {
2844
"icon": "mdi:map-marker-radius",
29-
"description": "Location presence",
45+
"description": "Location Presence",
3046
}
3147

3248
# Device automation
@@ -43,6 +59,6 @@
4359
ALL_USERS_ENTERED = "all_entered"
4460
ALL_USERS_LEFT = "all_left"
4561

46-
# Trigger events
62+
# Trigger events (these are fired in the bus, and available to the user)
4763
EVENT_USER_ENTERED = "crownstone.user_entered"
4864
EVENT_USER_LEFT = "crownstone.user_left"

0 commit comments

Comments
 (0)