So i’ve spent so much time on this, and now i’m trying to dumb it down as much as possible, but i’m just not able to interact with the StateMachine. It seems no matter what i try, i get
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 281, in _async_setup_platform
await asyncio.shield(task)
File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/config/custom_components/accounts/sensor.py", line 18, in setup_platform
add_entities([AccountsSensor(hass, entity_id)], False)
File "/config/custom_components/accounts/sensor.py", line 28, in __init__
self._stat = hass.states.get(self._entity_id).state
AttributeError: 'NoneType' object has no attribute 'state'
This is my current code:
import logging
import async_timeout
# from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'accounts'
ENTITY_ID = 'sun.sun'
def setup_platform(hass, config, add_entities, discovery_info=None):
entity_id = ENTITY_ID
add_entities([AccountsSensor(hass, entity_id)], False)
_LOGGER.info('Setup complete')
class AccountsSensor(Entity):
def __init__(self, hass, entity_id):
self._name = 'testname'
self._entity_id = entity_id
self._stat = hass.states.get(self._entity_id).state
@property
def name(self):
return self._name
@property
def state(self):
return self._state
Is there something that I’m not importing, or something i’m skipping?