Improve service by allowing to reference entity id instead of deconz id#11862
Improve service by allowing to reference entity id instead of deconz id#11862balloob merged 15 commits intohome-assistant:devfrom
Conversation
cbc4091 to
84d3e57
Compare
balloob
left a comment
There was a problem hiding this comment.
It's not clear to me why this is necessary to begin with?
There was a problem hiding this comment.
Eval is not allowed, too dangerous.
There was a problem hiding this comment.
Do you have suggestion about how I can get a dictionary here?
There was a problem hiding this comment.
The Issue here is that data is expected to be json compliant, which it unfortunately isn't since it get single quotes, where as json.loads expects double quotes. So there is a translation going on from the service entry in the gui to the service in the backend; {"name": "TRADFRI motion sensor 1"} arrives as {'name': 'TRADFRI motion sensor 1'}
There was a problem hiding this comment.
This is not allowed. Main component should not have access to the entity objects.
There was a problem hiding this comment.
I guess I could just do a translation dict between entity name and deconz id instead
|
The real issue is that there is no easy way for the user to know the deconZ_id. I could add it as an attribute to each entity or do you have an alternative to how I can do this? The important thing is to help the user being able to use the service with all devices in deconz. |
|
@balloob I'd be happy to implement according to your wishes or redesign when your entity registry has been delivered, can we move forward with this? |
facbb09 to
9da45a6
Compare
bcea029 to
9c902b9
Compare
9c902b9 to
f5b54fe
Compare
There was a problem hiding this comment.
block comment should start with '# '
There was a problem hiding this comment.
block comment should start with '# '
There was a problem hiding this comment.
I think what you're looking for is chain, that way you don't have to create a list but can just iterate over it.
from itertools import chain
for device in chain(deconz.groups.values(), deconz.lights.values(), …):There was a problem hiding this comment.
Not needed here, just get the entry. Do make sure to use guard clause pattern:
entry = registry.async_get_entry(…)
if entry is None:
_LOGGER.error(…)
return…gether with deconz id
Fixed call to protected member
74a1b1d to
757e1c2
Compare
There was a problem hiding this comment.
I'm sorry. I've thought about it more and I think that it's better to keep the Entity Registry private for now. So let have deconz keep track of unique id's.
You could do something like this fairly easy by leveraging the async_added_to_hass callback:
class DeconzLight(Light):
@asyncio.coroutine
def async_added_to_hass(self):
"""Called when entity added to hass."""
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self.unique_id(self.hass and self.entity_id are guaranteed to exist when async_added_to_hass is called)
|
@balloob alright, should I remove the new method for registry? Yes that was the original way I was doing it, just thought the registry would remove the need for a local registry. |
|
I know. Sorry for not realizing this earlier. Let's remove it. |
|
No worries, it is not always easy to have a plan ready for new functionality. I will change these things tonight. |
|
|
||
| import asyncio | ||
| import logging | ||
| from itertools import chain |
There was a problem hiding this comment.
'itertools.chain' imported but unused
|
|
||
| import asyncio | ||
| import logging | ||
| from itertools import chain |
There was a problem hiding this comment.
'itertools.chain' imported but unused
|
@balloob better like this? |
| """Representation of a binary sensor.""" | ||
|
|
||
| def __init__(self, sensor): | ||
| def __init__(self, hass, sensor): |
There was a problem hiding this comment.
Don't pass in hass. It will be set on the entity when the entity has been added to home assistant. This means you can access hass with self.hass, after the entity has been added to home assistant.
| def __init__(self, sensor): | ||
| def __init__(self, hass, sensor): | ||
| """Set up sensor and add update callback to get data from websocket.""" | ||
| self.hass = hass |
| """Representation of a deCONZ light.""" | ||
|
|
||
| def __init__(self, light): | ||
| def __init__(self, hass, light): |
There was a problem hiding this comment.
Don't pass in hass. It will be set on the entity when the entity has been added to home assistant. This means you can access hass with self.hass, after the entity has been added to home assistant.
| def __init__(self, light): | ||
| def __init__(self, hass, light): | ||
| """Set up light and add update callback to get data from websocket.""" | ||
| self.hass = hass |
| """Representation of a deCONZ scene.""" | ||
|
|
||
| def __init__(self, scene): | ||
| def __init__(self, hass, scene): |
| def __init__(self, scene): | ||
| def __init__(self, hass, scene): | ||
| """Set up a scene.""" | ||
| self.hass = hass |
| """Representation of a sensor.""" | ||
|
|
||
| def __init__(self, sensor): | ||
| def __init__(self, hass, sensor): |
| def __init__(self, sensor): | ||
| def __init__(self, hass, sensor): | ||
| """Set up sensor and add update callback to get data from websocket.""" | ||
| self.hass = hass |
| """Battery class for when a device is only represented as an event.""" | ||
|
|
||
| def __init__(self, device): | ||
| def __init__(self, hass, device): |
| def __init__(self, device): | ||
| def __init__(self, hass, device): | ||
| """Register dispatcher callback for update of battery state.""" | ||
| self.hass = hass |
|
Thanks @MartinHjelmare |
Description:
It is cumbersome to get ahold of the deconz_id, this allows the user to specify the entity_id instead.
Related issue (if applicable): fixes #
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4494
Checklist:
If user exposed functionality or configuration variables are added/changed: