Unable to import states from other entities

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?

So I found the reason and a nasty solution:

import logging

from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'accounts'
ENTITY_ID = 'input_text.test'

def setup_platform(hass, config, add_entities, discovery_info=None):
    temp = False
    while temp is False:
       if hass.states.get(ENTITY_ID):
           _LOGGER.error("Yes")
           state = hass.states.get(ENTITY_ID).state
           _LOGGER.error(state)
           temp = True
       else:
           _LOGGER.error("No")
    add_entities([AccountsSensor(state)], True)

class AccountsSensor(Entity):
    def __init__(self, state):
        self._name = 'test'
        self._state = state

    @property
    def name(self):
        return self._name

    @property
    def state(self):
        return self._state

How do handle these delays properly?